#include "DashboardUI_ViewGraph.h"
#include "ui_dashboard_viewgraph.h"

#include "Authentication.h"

#include <random>

DashboardUI_ViewGraph::DashboardUI_ViewGraph(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DashboardUI_ViewGraph)
{
    ui->setupUi(this);

    Authentication * auth = Authentication::getInstance();
    User * user = auth->getUser();

    connect(ui->widget, SIGNAL(rangeChanged(QDateTime,QDateTime)),
            this, SLOT(setDateFields(QDateTime,QDateTime)));

	connect(ui->dateStart, SIGNAL(editingFinished()),
            this, SLOT(setRange()));

    connect(ui->dateEnd, SIGNAL(editingFinished()),
            this, SLOT(setRange()));

    ui->typeGroup->setId(ui->bloodPressureSelect, Record::RECORD_DIA);
    ui->typeGroup->setId(ui->temperatureSelect, Record::RECORD_TEMP);
    ui->typeGroup->setId(ui->pulseSelect, Record::RECORD_PULSE);
    ui->typeGroup->setId(ui->respiratorySelect, Record::RECORD_RESP);
    ui->typeGroup->setId(ui->weightSelect, Record::RECORD_WEIGHT);

    if (user->getUserType() == User::PATIENT)
    {
        recordDB.loadRecords(user->getId());
        ui->widget->setRecordData(*recordDB.getRecords());
    }
    else
    {
        reload();
    }

    showRegLine = false;

    ui->analysisInfoGroup->hide();
    ui->hideRegButton->hide();
}

DashboardUI_ViewGraph::~DashboardUI_ViewGraph()
{
    delete ui;
}

void DashboardUI_ViewGraph::setDateFields(QDateTime start, QDateTime end)
{
    if (ui->dateStart->dateTime() != start)
        ui->dateStart->setDateTime(start);

    if (ui->dateEnd->dateTime() != end)
        ui->dateEnd->setDateTime(end);
}

void DashboardUI_ViewGraph::setRange()
{
    ui->widget->setDateRange(ui->dateStart->dateTime(),
                             ui->dateEnd->dateTime());
}

void DashboardUI_ViewGraph::reload()
{
    Authentication * auth = Authentication::getInstance();

    if (auth->getPatient() != nullptr)
    {
        recordDB.loadRecords(auth->getPatient()->getId());
        ui->widget->setRecordData(*recordDB.getRecords());
    }
}

void DashboardUI_ViewGraph::on_typeGroup_buttonClicked(int id)
{
    ui->widget->setRecordType(static_cast<Record::RecordField>(id));
}

void DashboardUI_ViewGraph::on_linearRegButton_clicked()
{
    ui->widget->showRegLine();

    RegLine reg = ui->widget->getRegressionLine();
    ui->changeValueLabel->setText(QString::number(reg.slope * 24, 'G', 3) +
                                  " " + reg.getUnitString() + " / day");

    if (!showRegLine)
    {
        ui->linearRegButton->setText("Recalculate");
        ui->analysisInfoGroup->show();
        ui->hideRegButton->show();

        showRegLine = true;
    }
}

void DashboardUI_ViewGraph::on_hideRegButton_clicked()
{
    if (showRegLine)
    {
        ui->widget->hideRegLine();
        ui->linearRegButton->setText("Show Trend Line");

        ui->analysisInfoGroup->hide();
        ui->hideRegButton->hide();

        showRegLine = false;
    }
}

void DashboardUI_ViewGraph::on_resetViewButton_clicked()
{
    ui->widget->resetDateRange();
}
